Imports System.Data.Common
Partial Class Presentation_Control_AssignExecutiveClients
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Sql As String
        Dim Records As DbDataReader
        Dim First As New ListItem

        Page.Title = "Assign one Executive's Clients to another"

        If Not Page.IsPostBack Then

            'Populate the Primary Executives List
            dlExecutivePrimary.DataTextField = "ExecutiveName"
            dlExecutivePrimary.DataValueField = "ExecutiveID"

            Sql = "SELECT ExecutiveID, ExecutiveName FROM ExecutiveMaster WHERE" & _
                    " Active = 1 ORDER BY ExecutiveName"

            dlExecutivePrimary.Items.Clear()
            First.Text = "Select an Executive"
            First.Value = "0"
            First.Selected = True
            Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
            dlExecutivePrimary.DataSource = Records
            dlExecutivePrimary.DataBind()
            dlExecutivePrimary.Items.Add(First)
            Records.Close()

        End If

    End Sub

    Protected Sub dlExecutivePrimary_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dlExecutivePrimary.SelectedIndexChanged

        Dim Sql As String
        Dim Records As DbDataReader
        Dim Params(0) As GenParameter

        'Populate the Secondary Executives List
        dlExecutiveSecondary.DataTextField = "ExecutiveName"
        dlExecutiveSecondary.DataValueField = "ExecutiveID"


        Sql = "SELECT ExecutiveID, ExecutiveName FROM ExecutiveMaster WHERE" & _
                " Active = 1 AND ExecutiveID <> ?PrimaryExecID ORDER BY ExecutiveName"

        Params(0) = New GenParameter("?PrimaryExecID", dlExecutivePrimary.SelectedValue, Data.DbType.Int32)

        Records = GenHelper.ExecuteReader(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
        dlExecutiveSecondary.DataSource = Records
        dlExecutiveSecondary.DataBind()
        Records.Close()
        dlExecutiveSecondary.Visible = True

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SecondaryExecutive As New Executive(dlExecutiveSecondary.SelectedValue)

        Try

            SecondaryExecutive.AssignExecutiveClients(dlExecutivePrimary.SelectedValue)

        Catch ex As Exception

            lblSuccess.Text = ex.Message
            lblSuccess.Visible = True
            lblSuccess.ForeColor = Drawing.Color.Red
            Exit Sub

        End Try

        lblSuccess.Visible = True
        lblSuccess.Text = "Successfully Assigned"
        lblSuccess.ForeColor = Drawing.Color.Black
    End Sub
End Class
